// pages/act/acadHome/acadHome.js
let {
  api_GetAcadActListOn,
  api_GetAcadActListOff,
  api_LikeAct
} = require("../../api/getData.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    dataSource: [],
    curPage: 1,
    maxSize: 0,
    dataSource_1: [],
    curPage_1: 1,
    maxSize_1: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    /*获取学术活动列表*/
    this.getAcadActList()

  },
  onReachBottom: function() {
    // 页面相关事件处理函数--监听下拉触底
    this.getAcadActList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.refresh()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.loadAcadActivies()
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  /**************以上是生命周期********************以下是自定义函数**************************/

  //滑动切换
  swiperTab: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },

  //点击切换
  clickTab: function(e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  /*获取学术活动列表*/
  // getAcadActList: function(e){
  //   console.log("==> 加载函数执行")
  //   console.log("bottom: ", this.data.bottom)
  //   if (this.data.bottom >= this.data.maxSize) {
  //     console.log("==> 到达底部");
  //     wx.showToast({
  //       title: '没有更多活动了！',
  //       icon: 'none',
  //     })
  //     return
  //   } else {
  //     let params = { page: this.data.curPage + 1, size: 10 };
  //     let acadList = this.data.dataSource_0
  //     api_GetAcadActListOn(params, (res) => {
  //       console.log("#########--学术活动列表--##########", res.data)
  //       let acadListOn, acadListOff = []
  //       //下拉刷新停止
  //       wx.stopPullDownRefresh()
  //       res.data.list.forEach(i => {
  //         acadList.push(i)
  //       })
  //       console.log("#########--学术活动列表--##########", acadList)
  //       acadListOn = acadList.filter(function (index) {
  //         return index.state === "正在报名";
  //       });
  //       console.log("正在报名的活动==>", acadListOn)
  //       acadListOff = acadList.filter(function (index) {
  //         return index.state === "已完成";
  //       });
  //       console.log("已完成的活动==>", acadListOff)
  //       this.setData({
  //         bottom: acadList.length,
  //         dataSource_0: acadList,
  //         dataSource: acadListOn,
  //         dataSource_1: acadListOff,
  //         curPage: res.data.curPage,
  //         maxSize: res.data.maxSize,
  //       })
  //     })

  //     api_GetAcadActListOff(params, (res) => {
  //       console.log("#########--学术活动列表--##########", res.data)
  //       let acadListOn, acadListOff = []
  //       //下拉刷新停止
  //       wx.stopPullDownRefresh()
  //       res.data.list.forEach(i => {
  //         acadList.push(i)
  //       })
  //       console.log("#########--学术活动列表--##########", acadList)
  //       acadListOn = acadList.filter(function (index) {
  //         return index.state === "正在报名";
  //       });
  //       console.log("正在报名的活动==>", acadListOn)
  //       acadListOff = acadList.filter(function (index) {
  //         return index.state === "已完成";
  //       });
  //       console.log("已完成的活动==>", acadListOff)
  //       this.setData({
  //         bottom: acadList.length,
  //         dataSource_0: acadList,
  //         dataSource: acadListOn,
  //         dataSource_1: acadListOff,
  //         curPage: res.data.curPage,
  //         maxSize: res.data.maxSize,
  //       })
  //     })

  //   }
  // },

  /*获取初始的学术活动列表*/
  getAcadActList: function(e) {
    /*获取可报名的学术活动列表*/
    let params = {
      page: 1,
      size: 6
    };
    api_GetAcadActListOn(params, (res) => {
      console.log("#########--可报名的学术活动--##########", res.data)
      this.setData({
        dataSource: res.data.list,
        bottom: res.data.list.length,
        curPage: res.data.curPage,
        maxSize: res.data.maxSize,
      })
    });

    /*获取已结束的活动列表*/
    let params_1 = {
      page: 1,
      size: 6
    };
    api_GetAcadActListOff(params_1, (res) => {
      console.log("#########--已结束的学术活动--##########", res.data)
      this.setData({
        dataSource_1: res.data.list,
        bottom_1: res.data.list.length,
        curPage_1: res.data.curPage,
        maxSize_1: res.data.maxSize,
      })
    });
    wx.stopPullDownRefresh();
  },

  /*上拉加载*/
  loadAcadActivies: function(e) {
    console.log("==> 加载函数执行")
    //先行判断是哪个tab
    if (this.data.currentTab == 0) {
      console.log("bottom: ", this.data.bottom)
      console.log("maxSize: ", this.data.maxSize)
      if (this.data.bottom >= this.data.maxSize) {
        console.log("==> 到达底部");
        wx.showToast({
          title: '别再拉了，我是有底线的！',
          icon: 'none',
        })
        return
      } else {
        let params = {
          page: this.data.curPage + 1,
          size: 6
        };
        let temp = this.data.dataSource
        api_GetAcadActListOn(params, (res) => {
          console.log("#########--加载的可报名学术活动列表--##########", res.data)
          res.data.list.forEach(i => {
            temp.push(i)
          })
          this.setData({
            bottom: temp.length,
            dataSource: temp,
            curPage: res.data.curPage,
            maxSize: res.data.maxSize,
          })
        })
      }
    } else {
      console.log("bottom_1 ", this.data.bottom_1)
      console.log("maxSize_1: ", this.data.maxSize_1)
      if (this.data.bottom_1 >= this.data.maxSize_1) {
        console.log("==> 到达底部");
        wx.showToast({
          title: '别再拉了，我是有底线的！',
          icon: 'none',
        })
        return
      } else {
        let params = {
          page: this.data.curPage_1 + 1,
          size: 6
        };
        let temp = this.data.dataSource_1
        api_GetAcadActListOff(params, (res) => {
          console.log("#########--加载的已结束学术活动列表--##########", res.data)
          res.data.list.forEach(i => {
            temp.push(i)
          })
          this.setData({
            bottom_1: temp.length,
            dataSource_1: temp,
            curPage_1: res.data.curPage,
            maxSize_1: res.data.maxSize,
          })
        })
      }
    }
  },
  /* 下拉刷新 */
  refresh: function() {
    console.log("==> 下拉刷新")
    this.setData({
      bottom: -1,
      dataSource_0: [],
      dataSource: [],
      dataSource_1: [],
      curPage: 0,
      maxSize: 0,
    })
    this.getAcadActList();
  },

  /*点击喜欢活动，只在微沙龙和活动广场首页显示*/
  loveButtonClick: function(e) {
    console.log('2222222222222222222222222222', e)
    let loveId = e.currentTarget.dataset.index.actId
    //渲染层样式改变
    if (e.currentTarget.dataset.index.state == '正在报名') {
      let index = 0
      this.data.dataSource.forEach(i => {
        if (i.actId == loveId) {
          let step = i.favoritedQuantity
          if (i.hasFavorited) {
            step--
          } else {
            step++
          }
          let str1 = 'dataSource[' + index + '].hasFavorited'
          let str2 = 'dataSource[' + index + '].favoritedQuantity'
          this.setData({
            [str1]: !i.hasFavorited,
            [str2]: step
          })
        }
        index++
      })
    } else {
      let index = 0
      this.data.dataSource_1.forEach(i => {
        if (i.actId == loveId) {
          let step = i.favoritedQuantity
          if (i.hasFavorited) {
            step--
          } else {
            step++
          }
          let str1 = 'dataSource_1[' + index + '].hasFavorited'
          let str2 = 'dataSource_1[' + index + '].favoritedQuantity'
          this.setData({
            [str1]: !i.hasFavorited,
            [str2]: step
          })
        }
        index++
      })
    }
    // 同步更改后台数据
    console.log('点击活动的actID==>', loveId)
    let param = 'actId='
    param += loveId
    api_LikeAct(param, (res) => {
      console.log('更改喜爱状态回调==>', res)
    })
  },

  //点击跳转活动详情页
  onClickTODO: function(e) {
    var $data = e.currentTarget.dataset.index;
    console.log('当前点击的活动ID==>', $data.actId)
    wx.navigateTo({
      url: '../actDetail/actDetail?actId=' + $data.actId,
      success: function(res) {
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },


})